import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/Home.vue'),
      meta: { 
        title: '首页',
        requiresAuth: false
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/auth/Login.vue'),
      meta: { 
        title: '登录',
        requiresAuth: false,
        hideForAuth: true
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/auth/Register.vue'),
      meta: { 
        title: '注册',
        requiresAuth: false,
        hideForAuth: true
      }
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: () => import('@/views/Dashboard.vue'),
      meta: { 
        title: '仪表板',
        requiresAuth: true
      }
    },
    {
      path: '/agents',
      name: 'agents',
      component: () => import('@/views/agents/AgentList.vue'),
      meta: { 
        title: '智能体',
        requiresAuth: true
      }
    },
    {
      path: '/agents/create',
      name: 'agent-create',
      component: () => import('@/views/agents/AgentCreate.vue'),
      meta: { 
        title: '创建智能体',
        requiresAuth: true
      }
    },
    {
      path: '/agents/:id',
      name: 'agent-detail',
      component: () => import('@/views/agents/AgentDetail.vue'),
      meta: { 
        title: '智能体详情',
        requiresAuth: true
      }
    },
    {
      path: '/chat/:agentId?',
      name: 'chat',
      component: () => import('@/views/chat/ChatInterface.vue'),
      meta: { 
        title: '对话',
        requiresAuth: true
      }
    },
    {
      path: '/conversations',
      name: 'conversations',
      component: () => import('@/views/chat/ConversationList.vue'),
      meta: { 
        title: '对话历史',
        requiresAuth: true
      }
    },
    {
      path: '/profile',
      name: 'profile',
      component: () => import('@/views/user/Profile.vue'),
      meta: { 
        title: '个人资料',
        requiresAuth: true
      }
    },
    {
      path: '/learning',
      name: 'learning',
      component: () => import('@/views/learning/CourseList.vue'),
      meta: { 
        title: '学习',
        requiresAuth: true
      }
    },
    {
      path: '/knowledge',
      name: 'knowledge',
      component: () => import('@/views/knowledge/KnowledgeBase.vue'),
      meta: { 
        title: '知识库',
        requiresAuth: true
      }
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('@/views/NotFound.vue'),
      meta: { 
        title: '页面未找到',
        requiresAuth: false
      }
    }
  ]
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - AI学习智能体门户` : 'AI学习智能体门户'
  
  // 检查认证状态
  if (to.meta.requiresAuth) {
    if (!userStore.isAuthenticated) {
      // 尝试从本地存储恢复token
      await userStore.checkAuth()
      
      if (!userStore.isAuthenticated) {
        next('/login')
        return
      }
    }
  }
  
  // 已登录用户访问登录/注册页面时重定向到仪表板
  if (to.meta.hideForAuth && userStore.isAuthenticated) {
    next('/dashboard')
    return
  }
  
  next()
})

export default router